package com.jic.market.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.market.common.constant.CommonConst;
import com.jic.market.common.enums.MerchantTypeEnum;
import com.jic.market.common.util.LoginService;
import com.jic.market.entity.PatchGrouponMerchantEntity;
import com.jic.market.entity.PromotionMerchantEntity;
import com.jic.market.mapper.PatchGrouponMerchantEntityMapper;
import com.jic.market.mapper.PatchGrouponMpEntityMapper;
import com.jic.market.mapper.PromotionMerchantEntityMapper;
import com.jic.market.model.LoginUser;
import com.jic.market.orm.Pagination;
import com.jic.market.request.PatchGrouponMerchantRequest;
import com.jic.market.request.PromotionMerchantRequest;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PatchGrouponMerchantResponse;
import com.jic.market.response.PromotionMerchantResponse;
import com.jic.market.service.PatchGrouponMerchantService;
import com.jic.market.service.PatchGrouponStockLmtService;
import com.jic.market.service.PromotionMerchantService;
import io.swagger.models.auth.In;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 促销商家店铺关系表
 * @author : guohuayong
 * @email : 
 * @since : 2020-01-14 08:00:40
 * @version : v1.0.0
 */
@Service
public class PatchGrouponMerchantServiceImpl implements PatchGrouponMerchantService {

    @Autowired
    private PatchGrouponMerchantEntityMapper curMapper;

    @Autowired
    private PatchGrouponMpEntityMapper mpEntityMapper;

    @Autowired
    private PatchGrouponStockLmtService patchGrouponStockLmtService;

    @Override
    public PageResponse<PatchGrouponMerchantResponse> listByPage(PatchGrouponMerchantRequest request) {
        PageResponse<PatchGrouponMerchantResponse> responsePageResponse = new PageResponse<PatchGrouponMerchantResponse>()
                .setPageNo(request.getPageNo())
                .setPageSize(request.getPageSize());
        Pagination pagination = Pagination.build(request);

        PatchGrouponMerchantEntity promotionEntity = new PatchGrouponMerchantEntity();
        promotionEntity.setPromotionId(request.getPromotionId());
        promotionEntity.setMerchantType(request.getMerchantType());

        AdminOperate adminOperate = request.getAdminOperate();
        promotionEntity.setPromotionId(request.getPromotionId());
        promotionEntity.setMerchantType(request.getMerchantType());
        promotionEntity.setMerchantName(request.getMerchantName());
        promotionEntity.setStorecode(request.getStorecode());
        promotionEntity.setStorename(request.getStorename());
        promotionEntity.setOrgNumber(request.getOrgNumber());


        DeptTypeEnums deptType = adminOperate.getDeptType();
        List<Long> merchantIds = null;
        List<Long> storeIds = null;
        // 根据平台判断数据权限   开始
//        if(deptType != null){
//            Long bizId = adminOperate.getBusinessId();
//            //母平台
//            if(deptType.equals(DeptTypeEnums.OPERATION_PLATFORM) || deptType.equals(DeptTypeEnums.CHILD_PLATFORM)){
//                merchantIds = adminOperate.getMerchantIds();
//                storeIds = adminOperate.getStoreIds();
//                if(CollectionUtils.isEmpty(merchantIds)){
//                    responsePageResponse.setTotal(0);
//                    return responsePageResponse;
//                }
//                if(CollectionUtils.isEmpty(storeIds)){
//                    responsePageResponse.setTotal(0);
//                    return responsePageResponse;
//                }
//            }else if(deptType.equals(DeptTypeEnums.MERCHANT_PLATFORM)){
//                merchantIds = Arrays.asList(bizId);
//                storeIds = adminOperate.getStoreIds();
//                if(CollectionUtils.isEmpty(merchantIds)){
//                    responsePageResponse.setTotal(0);
//                    return responsePageResponse;
//                }
//                if(CollectionUtils.isEmpty(storeIds)){
//                    responsePageResponse.setTotal(0);
//                    return responsePageResponse;
//                }
//            }else if(deptType.equals(DeptTypeEnums.STORE_PLATFORM)){
//                storeIds = Arrays.asList(bizId);
//                if(CollectionUtils.isEmpty(storeIds)){
//                    responsePageResponse.setTotal(0);
//                    return responsePageResponse;
//                }
//            }
//        }

        // 根据平台判断数据权限   结束

        //商家
//        if(request.getMerchantType() == 1){
//            promotionEntity.setMerchantIds(merchantIds);
//        }else {
//            promotionEntity.setStoreIds(storeIds);
//        }

        List<PatchGrouponMerchantEntity> entityList = curMapper.findOfPage(promotionEntity, pagination);
        int count = curMapper.countOfPage(promotionEntity);

        responsePageResponse
                .setPageNo(request.getPageNo())
                .setPageSize(request.getPageSize())
                .setTotalCount(count)
                .setList(entityList.stream().map(e->{
                    PatchGrouponMerchantResponse response = new PatchGrouponMerchantResponse();
                    response.setId(e.getId());
                    response.setPromotionId(e.getPromotionId());
                    response.setMerchantId(e.getMerchantId());
                    response.setMerchantType(e.getMerchantType());
                    response.setMerchantName(e.getMerchantName());
                    response.setStoreId(e.getStoreId());
                    response.setDeleteFlag(e.getDeleteFlag());
                    response.setCreateTime(e.getCreateTime());
                    response.setCreator(e.getCreator());
                    response.setCreatorName(e.getCreatorName());
                    response.setUpdateTime(new Date());
                    response.setUpdator(e.getUpdator());
                    response.setUpdatorName(e.getUpdatorName());
                    response.setOrgNumber(e.getOrgNumber());
                    response.setMerchantTypeManager(e.getMerchantTypeManager());
                    response.setStorecode(e.getStorecode());
                    response.setStorename(e.getStorename());
                    response.setStoreType(e.getStoreType());

                    return response;
                }).collect(Collectors.toList()));
        return responsePageResponse;
    }

    @Override
    public Integer deleteByPromotionId(PatchGrouponMerchantRequest request) {
        PatchGrouponMerchantEntity entity = new PatchGrouponMerchantEntity();
        entity.setPromotionId(request.getPromotionId());
        entity.setDeleteFlag(CommonConst.DeleteEnum.DELETE.getCode());
        entity.setMerchantType(request.getMerchantType());

        return curMapper.updateByPromotionId(entity);
    }

    @Autowired
    LoginService loginService;

    @Override
    public Integer saveBatchByPromotionId(List<PatchGrouponMerchantRequest> pomotionList) {
        if (null == pomotionList || pomotionList.isEmpty()) {
            return 0;
        }
//        LoginUser currentLoginUser = loginService.getCurrentLoginUser();

        for (int i = 0; i < pomotionList.size(); i++) {
            PatchGrouponMerchantRequest temp = pomotionList.get(i);
            PatchGrouponMerchantEntity query = new PatchGrouponMerchantEntity();
            query.setDeleteFlag(0);
            query.setPromotionId(temp.getPromotionId());
            if(temp.getMerchantType() == 1){//商家
                query.setMerchantId(temp.getMerchantId());
            }else{
                query.setStoreId(temp.getStoreId());
            }

            if(curMapper.countOfPage(query) == 0) {
                PatchGrouponMerchantEntity p = new PatchGrouponMerchantEntity();
                p.setPromotionId(temp.getPromotionId());
                p.setMerchantId(temp.getMerchantId());
                p.setMerchantType(temp.getMerchantType());
                p.setMerchantName(temp.getMerchantName());
                p.setStoreId(temp.getStoreId());
                p.setCreateTime(new Date());
//            p.setCreator(currentLoginUser.getId());
//            p.setCreatorName(currentLoginUser.getUsername());
                p.setUpdateTime(new Date());
//            p.setUpdator(currentLoginUser.getId());
//            p.setUpdatorName(currentLoginUser.getUsername());
                p.setOrgNumber(temp.getOrgNumber());
                p.setMerchantTypeManager(temp.getMerchantTypeManager());
                p.setStorename(temp.getStorename());
                p.setStorecode(temp.getStorecode());
                p.setStoreType(temp.getStoreType());

                int ret = curMapper.insert(p);
            }
//            if (ret < 1) {
//                return 0;
//            }
        }

        return 1;
    }

    @Override
    public Integer deleteById(Long id) {
        // 查询商家或者店铺信息,
        PatchGrouponMerchantEntity saleCondition =new PatchGrouponMerchantEntity();
        saleCondition.setId(id);
        PatchGrouponMerchantEntity sale = curMapper.query(saleCondition);
        // 获取促销id
        Long promotionId =sale.getPromotionId();

        // 判断是否是商家
        List<Long> storeIds =new ArrayList<>();
        // 如果是商家则获取商家id
        Long merchantId =null;
        if(MerchantTypeEnum.MERCHANT.getCode().equals(sale.getMerchantType())){
            merchantId =sale.getMerchantId();

            // 获取商家id下的店铺集
            List<Long> merchantIds =new ArrayList<>();
            merchantIds.add(merchantId);
            List<PatchGrouponMerchantEntity> storeListDB = curMapper.findAllStoreByMerchantIds(promotionId, merchantIds);

            for (PatchGrouponMerchantEntity pgStore : storeListDB) {
                storeIds.add(pgStore.getStoreId());
            }
            // 删除商家下的店铺
            if(merchantIds != null && merchantIds.size()>0){
                curMapper.deleteStoreListByMerchantIds(promotionId, merchantIds);
            }

        }

        // 如果是店铺则获取店铺id
        Long storeId =null;
        if(MerchantTypeEnum.STORE.getCode().equals(sale.getMerchantType())){
            storeId =sale.getStoreId();
            storeIds.add(storeId);
        }

        // 删除店铺下的所有促销商品，promotionId +条件为店铺ids
        if (storeIds != null && storeIds.size()>0){
            mpEntityMapper.deleteEntityListByPromotionIdAndStoreIdsBatch(promotionId, storeIds);
            patchGrouponStockLmtService.deleteEntityListByPromotionIdAndStoreIdsBatch(promotionId, storeIds);
        }

        PatchGrouponMerchantEntity entity = new PatchGrouponMerchantEntity();
        entity.setId(id);
        entity.setDeleteFlag(CommonConst.DeleteEnum.DELETE.getCode());

        return curMapper.update(entity);
    }

    @Override
    public List<PatchGrouponMerchantEntity> findAll(PatchGrouponMerchantEntity condition) {
        return curMapper.findAll(condition);
    }

    @Override
    public Integer insert(PatchGrouponMerchantEntity condition) {
        return curMapper.insert(condition);
    }

    @Override
    public int insertBatch(List<PatchGrouponMerchantEntity> patchGrouponMerchantEntityList) {
        return curMapper.insertBatch(patchGrouponMerchantEntityList);
    }
}